package com.test.shopping.controller;

import com.test.shopping.pojo.List;
import com.test.shopping.pojo.User;
import com.test.shopping.service.userService;
import com.test.shopping.service.listService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@Slf4j
public class userController {
    @Autowired
    private userService userService;
    @Autowired
    private listService listService;

    //查询用户钱包余额
    @GetMapping("getUserBalance/{uId}")
    public User getUserBalance(@PathVariable Integer uId){
        User user = new User();
        user.setUId(uId);
        return userService.getUserBalance(user);
    }
    //用户消费100元
    @PutMapping("consume/{uId}")
    public String consume(@PathVariable Integer uId){
        try {
            User user = new User();
            user.setUId(uId);
            userService.consume(user);
            List list = new List();
            list.setList(+100);
            list.setUId(uId);
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            list.setDate(dateFormat.format(date));
            listService.selfInsert(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "消费成功";
    }
    //用户退款20元
    @PutMapping("refund/{uId}")
    public String refund(@PathVariable Integer uId){
        try {
            User user = new User();
            user.setUId(uId);
            userService.refund(user);
            List list = new List();
            list.setList(-20);
            list.setUId(uId);
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            list.setDate(dateFormat.format(date));
            listService.selfInsert(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "退款成功";
    }
}
